package com.cssw.fyzb.tps.api.yspay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cssw.fyzb.tps.api.yspay.utils.*;
import com.huifu.adapay.core.util.AdapaySign;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2021/1/25 20:26
 */
@Function(description = "银盛支付验签", parameters = {
        @Parameter(name = "data", type = Type.String, description = "验签数据", required = true)
})
@Component
public class VerifyYespaySign implements Action {

    @Override
    public Response execute(Request request) throws Exception {
        String data = request.getString("data");
        try {
            Map<String, String> reqMap = getReqMap(data);
            Map map = new HashMap();
            map.put("checkSign", YsPaySignUtils.asynVerifyYs(reqMap));
            return Response.success().put("data", map);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail("验签失败");
        }
    }

    private Map<String, String> getReqMap(String param) {
        JSONObject jsonObject = JSON.parseObject(param);
        Map<String, String> map = new HashMap<>();
        for (String key: jsonObject.keySet()){
            map.put(key, jsonObject.getString(key));
        }

        return map;
    }
}
